package org.njyinghuo.sdk.bytedance.client.douyin;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.njyinghuo.sdk.bytedance.conf.Config;
import org.njyinghuo.sdk.bytedance.exception.DouYinException;

import java.io.File;

/**
 * @description:
 * @author: Hupengpeng
 * @time: 2021/7/6 16:09
 */
public class ImageUpload {
    private String open_id;
    private String access_token;
    private File image;
    private String response;

    private ImageUpload() {
    }

    protected static ImageUpload New() {
        return new ImageUpload();
    }

    public ImageUpload execute() {
        String url = StrUtil.format("{}{}?open_id={}&access_token={}",
                Config.API_HTTP_SCHEME + Config.API_HOST,
                Config.API_IMAGE_UPLOAD,
                getOpen_id(),
                getAccess_token());
        HttpResponse rs = HttpUtil.createPost(url).form("image", getImage()).execute();
        response = rs.body();
        return this;
    }
    public ImageUploadResponse convert() {
        try {
            JSONObject obj = JSONUtil.parseObj(response);
            JSONObject data = obj.getJSONObject("data");
            if (obj.getJSONObject("extra").getInt("error_code") == 0) {
                return JSONUtil.toBean(data.getStr("image"), ImageUploadResponse.class);
            }
        } catch (Exception e) {
            throw new DouYinException(StrUtil.format("{}:{}", e.getMessage(), response));
        }
        throw new DouYinException(response);
    }
    public String getOpen_id() {
        return open_id;
    }

    public ImageUpload setOpen_id(String open_id) {
        this.open_id = open_id;
        return this;
    }

    public String getAccess_token() {
        return access_token;
    }

    public ImageUpload setAccess_token(String access_token) {
        this.access_token = access_token;
        return this;
    }

    public File getImage() {
        return image;
    }

    public ImageUpload setImage(File image) {
        this.image = image;
        return this;
    }

    public String getResponse() {
        return response;
    }

    public ImageUpload setResponse(String response) {
        this.response = response;
        return this;
    }

    public class ImageUploadResponse {
        private int height;
        private String image_id;
        private int width;

        public int getHeight() {
            return height;
        }

        public ImageUploadResponse setHeight(int height) {
            this.height = height;
            return this;
        }

        public String getImage_id() {
            return image_id;
        }

        public ImageUploadResponse setImage_id(String image_id) {
            this.image_id = image_id;
            return this;
        }

        public int getWidth() {
            return width;
        }

        public ImageUploadResponse setWidth(int width) {
            this.width = width;
            return this;
        }
    }
}
